/**
 * Copyright (c) 2010 BullionXpress.com,
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */

package com.bullionxpress.client.view;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.text.*;
import java.util.HashMap;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;


public class AccountCellRenderer extends DefaultTableCellRenderer {

    public static final int ALIGN_LEFT = 0;
    public static final int ALIGN_RIGHT = 1;
    public static final int ALIGN_CENTER = 2;
    private static final long serialVersionUID = 1L;
    private Color evenRowColor = null;
    private Color oddRowColor = null;
    private Color defaultCellColor = null;
    private Color dataFontColor = null;
    private Font dataFont = null;
    public int Alignment = SwingConstants.CENTER;
    public int align = ALIGN_LEFT; //SwingConstants.CENTER;

    HashMap m = new HashMap();
    public AccountCellRenderer() {
        this(ALIGN_RIGHT);
    }

    public AccountCellRenderer(int align) {
        this.align = align;
        //setEvenRowColor(new Color(203, 201, 153));
        //setOddRowColor(new Color(224, 220, 193));
        //setDefaultCellColor(new Color(165, 164, 136));
        //setCellFontColor(new Color(0, 0, 0));

    }

    public void setCellFont(Font inFont) {
        this.dataFont = inFont;
    }

    public void setCellFontColor(Color inColor) {
        this.dataFontColor = inColor;
    }

    public void setEvenRowColor(Color inColor) {
        this.evenRowColor = inColor;
    }

    public void setOddRowColor(Color inColor) {
        this.oddRowColor = inColor;
    }

    public void setDefaultCellColor(Color inColor) {
        this.defaultCellColor = inColor;
    }
    JTable table = null;
    int row = -1;
    int col = -1;
    Object val = null;

    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
            boolean hasFocus, int row, int column) {


        Component cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        //System.out.println(value);
        super.setHorizontalAlignment(Alignment);
        val = value;
        cell.setForeground(Color.black);
        this.table = table;
        this.row = row;
        this.col = column;

        if (row % 2 == 0) {
            if (this.oddRowColor != null) {
                cell.setBackground(this.oddRowColor);
            } else if (this.defaultCellColor != null) {
                cell.setBackground(this.defaultCellColor);
            }
        } else {
            if (this.evenRowColor != null) {
                cell.setBackground(this.evenRowColor);
            } else if (this.defaultCellColor != null) {
                cell.setBackground(this.defaultCellColor);
            }
        }
        if (this.dataFont != null) {
            cell.setFont(this.dataFont);
        }
        if (this.dataFontColor != null) {
            cell.setForeground(this.dataFontColor);
        }
        if (isSelected) {
            //cell.setBackground(new Color(60, 60, 60));
            cell.setForeground(Color.black);

        }

        return cell;
    }

    public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;

        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setColor(getForeground());
        // if left then...
        int padding = 5;

        String str = getText();
        boolean isSelected = false;
        if (table != null) {
            int nrow = table.getSelectedRow();
            if (nrow == row) {
                isSelected = true;
            }
        }

        try {
            if( val instanceof Long ){
                long d = Long.parseLong(str);
                DecimalFormat df = new DecimalFormat("#,###,##0");
                String v = df.format(d);
                setText(v);
            }
            else if(val instanceof Double)
            {
                double d = Double.parseDouble(str);
                DecimalFormat df = new DecimalFormat("#,###,##0.00");
                String v = df.format(d);
                if( new Double(d).isNaN()){
                    v = "-";
                }
                setText(v);
            }
        } catch (Exception ex) {
        }
        if (align == ALIGN_LEFT) {
            g2d.drawString(getText(), padding, getFont().getSize());
            super.validate();
        } else if (align == ALIGN_RIGHT) {

            g2d.drawString(getText(), getWidth() - g2d.getFontMetrics().stringWidth(getText()) - padding, getFont().getSize());
            super.validate();
        } else if (align == ALIGN_CENTER) {

            g2d.drawString(getText(), (getWidth() / 2) - (g2d.getFontMetrics().stringWidth(getText()) / 2), getFont().getSize());
            super.validate();
        }
    }

    public void validate() {
    }

    public void revalidate() {
    }

    protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
    }

    public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) {
    }
}
